<template>
	<view class="safe-area-inset-bottom">
		<view class="swip_header">
			<view class="slot-box">
				<view style="width: 100%;">
					<uni-search-bar v-model="queryParams.name" type="text" @confirm="handleQuery" @input="handleQuery" placeholder="请输入类型姓名" />
				</view>
			</view>
			<view class="swiplist">
				<view>合同类型</view>
				<button size="mini" class="add_btn" @click="handleAdd"><i class="iconfont" style="color: #00a660;">&#xeaf3;</i>新增</span></button>
			</view>
		</view>
		
		<!-- 列表 -->
		<view v-if="contractTypeList != ''" class="listNameTop cardList">
		  <uni-card :is-shadow="false" v-for="(item,index) in contractTypeList" :key="index">
			<p class="grid-integral">类型名称：{{ item.name }}</p>
			<p class="grid-integral">排序：{{ item.sort }}</p>
			<p class="grid-integral">系统类型：
			<template v-for="(dictItem, index) in dicts.ec_is_system">
			  <template v-if="item.isSystem == dictItem.value">
			    <uni-tag :text="dictItem.text" :type="dictItem.type"/>
			  </template>
			</template>
			</p>
			<view slot="actions" class="card-actions">
			  <view>创建时间：{{item.createTime}}</view>
			  <view class="flex_between">
			  <view class="card-actions-item" @click="handleEdit(item)">
			    <text class="card-actions-item-text themeColor">编辑</text>
			  </view>
			  <view class="card-actions-item" @click="handleDelete(item,index)" v-if="item.isSystem == 0">
			    <text class="card-actions-item-text errorColor">删除</text>
			  </view>
			  </view>
			</view>	
		</uni-card>
		<!-- 加载更多 -->
		<uni-load-more color="#00a660" :status="loadStatus" />
		</view>
		<!-- 暂无数据 -->
		<luanqingEmpty :show="true" text="没有更多数据啦" v-else />
		<!-- 修改 查看详情 -->
		<uni-popup ref="ListPopup" background-color="#fff" type="dialog">
			<uni-popup-dialog :before-close="true" :type="msgType" cancelText="取消" confirmText="确定" :title="title" @confirm="dialogConfirm" @close="closed">
			<template v-slot="content">
				<uni-forms ref="form" :rules="rules" :modelValue="form">
				<uni-forms-item label="类型名称" required name="name">
						<uni-easyinput v-model="form.name" placeholder="请输入类型名称" :clearable="false" />
					</uni-forms-item>
					<uni-forms-item label="排序" required name="sort">
						<uni-number-box v-model="form.sort" />
					</uni-forms-item>
				</uni-forms>						
			</template>
			</uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
	  addContractType,
	  delContractType,
	  getContractType,
	  listContractType,
	  updateContractType
	} from "@/api/ec/console/busiConfig/contractType";
	import {parseTime} from "@/utils/common";
	import {getDictsByNameForUni} from "@/utils/dicts";
	import luanqingEmpty from '@/components/luanqing-empty/luanqing-empty';
	export default {
		components:{luanqingEmpty},
		data() {
			return {
				// 数据字典
				dicts: {
				  ec_is_system: [],
				},
				total:0, //总条数
				loadStatus:'loading', //是否加载中 //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
				msgType:'info',
				contractTypeList: [], // 合同类型列表
				title: "", // 弹出层标题
				// 查询参数
				queryParams: {
				  pageNum: 1,
				  pageSize: 10,
				  name: null,
				  isSystem: null,
				},
				form: {},
				rules: {
					name: {
						rules: [{
								required: true,
								errorMessage: '请输入类型名称'
							},{
								minLength: 2, maxLength: 12,
								errorMessage: '长度在2-12个，不能有特殊字符',
								trigger: "change"
							}]
						},
						sort: {
							rules: [{
								required: true,
								errorMessage: '请输入排序'
							}]
						},					
				},
			}
		},
		onLoad() {
			this.getList();
			this.getDicts()
		},
		//上拉触底函数
		onReachBottom() {
		  if (this.loadStatus != 'noMore' && this.total > this.contractTypeList.length) {
		    this.loadStatus = 'loading';
		    // 延迟500毫秒显示loading效果
		    setTimeout(() => {
		      // 执行查询的方法
		      this.getList(false);
		    }, 500)
		  } else {
		    // 停止加载
		    this.loadStatus = 'noMore'
		  }
		},
		methods: {
			/**加载数据字典*/
			getDicts() {
			  this.dicts.ec_is_system = getDictsByNameForUni("ec_is_system");
			},
			closed(){
				this.$refs.ListPopup.close()
			},
			/** 搜索 */
			handleQuery(){
				this.getList(true);
			},
			/** 获取合同类型列表 */
			getList(reload) {
			  if (reload) {
			    this.queryParams.pageNum = 1;
			    this.loadStatus = 'more'
          this.$modal.loading();
			  }
			  listContractType(this.queryParams).then(response => {
			    if (reload) {
			      uni.hideLoading()
			    }
			    this.total = response.total;
			    if (response.total > 0) {
			      const dataRows = response.rows;
			      this.contractTypeList = reload ? dataRows : this.contractTypeList.concat(dataRows);
			    } else {
			      this.contractTypeList = [];
			    }
			    // 判断数据是否加载完全
			    if (response.total <= this.contractTypeList.length) {
			      this.loadStatus = 'noMore'
			    }
			    // 页数+1
			    this.queryParams.pageNum++
			  }).catch(() => {
			    if (reload) {
			      uni.hideLoading()
			    }
			  });
			},
			/** 确定修改合同类型 */
			dialogConfirm(){
			    this.$refs.form.validate().then((res) => {
			      if (this.form.id != null) {
			            updateContractType(this.form).then(response => {
			              this.$modal.msgSuccess("修改成功");
			              this.getList(true);
						  this.closed()
			            });
			          } else {
			            addContractType(this.form).then(response => {
			              this.$modal.msgSuccess("新增成功");
			              this.getList(true);
						  this.closed()
			            });
			          }
			      });	        
			},
			/** 修改 */
			handleEdit(item,index,e){
				getContractType(item.id).then((response)=>{
					this.form = response.data;
					this.title = '修改合同类型'
					this.$refs.ListPopup.open('center')
				})	
			},
			/** 删除 */
			handleDelete(item,index){
				if (item.isSystem == 1) {
					    this.$modal.msgError("系统类型无法删除！");
					    return;    
					}
					let that = this
					// 删除
					uni.showModal({
						title: '系统提示',
						confirmColor:'#00a660',
						content: '是否确认删除该合同类型？',
						success: function (res) {
							if (res.confirm) {
								delContractType(item.id);
								that.contractTypeList.splice(index,1)
								uni.showToast({title: '删除成功'});
							} else if (res.cancel) {
							}
						 },
					})
			},
			/** 新建合同类型 */
			handleAdd(){
				 this.form = {
				        id: null,
				        name: null,
				        sort: 0
				      };
				this.title = '新增合同类型'
				this.$refs.ListPopup.open('center')
			},
		}
	}
</script>

<style scoped lang="scss">
	::v-deep .uni-forms-item__label{width: 150rpx !important;}
	::v-deep .uni-popup .uni-popup__wrapper{width: 90% !important;padding:0;}
</style>
